<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>还款申请</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div id="pageInTab" class="pageInTab">
			<form id="fnc_prj_pay_schfm_form">
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">项目基础信息</span>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目名称</div>
						<div class="field">
							<span name="FNCPRJNM" id="FNCPRJNM" >${bscinfo.FNCPRJNM}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目编号</div>
						<div class="field">
							<span name="FNCPRJID" id="FNCPRJID" >${bscinfo.FNCPRJID}</span>
							<input type="hidden" id="payschid" name="payschid" value="${bscinfo.SCHDID}">
							<input type="hidden" id="oper" name="oper" value="${oper}">
							<input type="hidden" id="jrnlid" name="jrnlid" value="${jrnlid}">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">创建日期</div>
						<div class="field">
							<span name="CRTTM" id="CRTTM" >${bscinfo.CRTTM}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资机构</div>
						<div class="field">
							<span name="FUNDRASINSNM" id="FUNDRASINSNM" >${bscinfo.FUNDRASINSNM}</span>
							<input type="hidden" id="fundrasinsid" name="fundrasinsid" value="${bscinfo.FUNDRASINSID}">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资方案</div>
						<div class="field">
							<span name="FNCSCMNM" id="FNCSCMNM" >${bscinfo.FNCSCMNM}</span>
							<input type="hidden" id="fncscmid" name="fncscmid" value="${bscinfo.FNCSCMID}">
							<input type="hidden" id="repytp" name="repytp" value="${bscinfo.REPY_TP}">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目金额</div>
						<div class="field">
							<span name="FNCPRJAMT" id="FNCPRJAMT" >${bscinfo.FNCPRJAMT}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">实际融资金额</div>
						<div class="field">
							<span name="FNCFIAMT" id="FNCFIAMT" >${bscinfo.ACFIAMT}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">支付表数量</div>
						<div class="field">
							<span name="PAYCNT" id="PAYCNT" >${bscinfo.PAYCNT}</span>
						</div>
					</div>
				</div>
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">账户信息</span>
					</div>
					<div class="fieldBox col_50">
						<div class="label">机构收款账户</div>
						<div class="field">
							<%-- <input name="recvAcc" id="recvAcc" type="text" class="ui-textbox" value="${raccno}"  validate="{required:true}"> --%>
							<a href='#' onclick='showInsAcc(${bscinfo.RACCID})'> <font color='blue'>${raccno}</font></a>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">我司付款账户</div>
						<div class="field">
							<%-- <input name="payAcc" id="payAcc" type="text" class="ui-textbox" value="${paccno}" > --%>
							<a href='#' onclick='showSysAcc(${bscinfo.PACCID})'> <font color='blue'>${paccno}</font></a>
						</div>
					</div>
				</div>	
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">融资端费用</span>
					</div>
					<div class="fieldBox col_100">
							<div id="cost0" class="sq-grid">
							</div>
					</div>
				</div>
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">还款明细</span>
					</div>
					<div class="fieldBox col_100">
							<div id="cost1" class="sq-grid">
							</div>
					</div>
				</div>
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="fieldBox col_100">
						<div class="label">还款本金合计</div>
						<div class="field">
							<input name="sumFeeAmt" id="sumFeeAmt" type="text" class="ui-textbox" disabled="disabled">
						</div>
					</div>
				</div>
			</form>
		</div>
<!-- 		<div class="btnsBox" id="ss">
			<span id="repay_list_add_btn_id" class="sq-button sq-button-round" >确认已付款保存</span>
		</div> -->
	</div>
<!-- 嵌入账户详情 -->
<jsp:include page="../mrgnmanageinfo/showAcc.jsp"></jsp:include>
	<script type="text/javascript">
	var griddata0,griddata1;
	$(function () {
		//f_initDatePicker();
		initCost0Grid();
		initDetlGrid();
    	$("#fnc_prj_pay_schfm_form").ligerForm({
    		validate:true
        });
    	saveFncPrjPayEvent();
	});
/*     function f_initDatePicker(){
    	var actpymtdt = $("#actpymtdt").val();

		var stDateStr = "";
		if(null != actpymtdt && actpymtdt != ""){
        	$("#actpymtdt").ligerDateEditor({ 
        		showTime: true, labelWidth: 100, labelAlign: 'left' ,format: 'yyyy-MM-dd',
        		onChangeDate : function(value){
        			$("#actpymtdt").val(value);
        		}
        	});
			var date = new Date(actpymtdt);
			stDateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        	$("#actpymtdt").ligerGetDateEditorManager().setValue(stDateStr);
		}
    } */
    function initCost0Grid()
    {
    	var jrnlid = $("#jrnlid").val();
    	griddata0 = $("#cost0").ligerGrid({
	    	url: '<%=path%>/rfbase/fncprjpaysch/initCost0JrnlGrid.do?jrnlid='+jrnlid,
	    	method : 'get',
            columns: [
                { display: '费用名称', name: 'COST_NM', width: 160, type: 'text'},
                { display: '应还金额', name: 'FEE_AMT', width: 160, type: 'float'},
                { display: '已还金额', name: 'PAID_UP_AMT', width: 160, type: 'float'},
                { display: '剩余金额', name: 'AR_AMT', width: 160, type: 'float',
                    totalSummary:
                    {
                        render: function (suminf, column, cell)
                        {
                            return '<div>合计:' + suminf.sum + '</div>';
                        },
                        align: 'left'
                    }
    			},
                { display: '应还金额浮动', name: 'FLOAT_AMT',type: 'float', 
                    editor: { type: 'float' }, align: 'left', width: 300
                },
                { display: '本次还款额', name: 'ACTUAL_AMT',type: 'float', 
                    editor: { type: 'float' }, align: 'left', width: 300,
                    totalSummary:
                    {
                        render: function (suminf, column, cell)
                        {
                            return '<div>' + suminf.sum + '</div>';
                        },
                        align: 'left'
                    }
                },
                { display: '机构收款账号', name: 'rcvpymtAccNoHtml', width: 150, type: 'text'}, 
                { display: '我司付款账户', name: 'payAccNm', width: 200, type: 'text'},
                { display: '我司付款账号', name: 'payAccNoHtml', width: 200, type: 'text'},
                { display: '备注', name: 'FEE_REM', width: 200, type: 'text',align: 'left'
                },
                { display: '逻辑序号', name: 'ID',width:0, align:'left', hide:true}
            ],
            enabledEdit: false, isScroll: true,rownumbers:true,
            width: '100%',height:160,checkbox:true,usePager:false
        });
    }
	
    function initDetlGrid()
    {
    	var jrnlid = $("#jrnlid").val();
    	griddata1 = $("#cost1").ligerGrid({
	    	url: '<%=path%>/rfbase/fncprjpaysch/initDetlJrnlGrid.do?jrnlid='+jrnlid,
	    	method : 'get',
            columns: [
            { display: '期数', name: 'payPd', width: 50, type: 'int' },
            { display: '应还款项', name: 'feeClsCd',width: 150, type: 'text',
                render: function (item)
                {
                    if (parseInt(item.feeClsCd) == 1) return '本金';
                    if (parseInt(item.feeClsCd) == 2) return '利息';
                    if (parseInt(item.feeClsCd) == 3) return '超额抵押';
                    return '其他费用';
                }},
            { display: '还款日', width: 150, name: 'payDt',render:renderDate},
            { display: '应还金额', name: 'feeAmt', width: 160, type: 'float'},
            { display: '已还金额', name: 'paidUpAmt', width: 160, type: 'float'},
            { display: '剩余金额', name: 'arAmt', width: 160, type: 'float',
                totalSummary:
                {
                    render: function (suminf, column, cell)
                    {
                        return '<div>合计:' + suminf.sum + '</div>';
                    },
                    align: 'left'
                }
			},
            { display: '应还金额浮动', name: 'floatAmt',type: 'float', 
                editor: { type: 'float' }, align: 'left', width: 300,
            },
            { display: '本次还款额', name: 'actualAmt',type: 'float', 
                editor: { type: 'float' }, align: 'left', width: 300,
                totalSummary:
                {
                    render: function (suminf, column, cell)
                    {
                        return '<div>' + suminf.sum + '</div>';
                    },
                    align: 'left'
                }
            },
            { display: '机构收款账号', name: 'rcvpymtAccNoHtml', width: 150, type: 'text' }, 
            { display: '我司付款账户', name: 'payAccNm', width: 200, type: 'text'},
            { display: '我司付款账号', name: 'payAccNoHtml', width: 200, type: 'text'},
            { display: '备注', name: 'feeRem', width: 200, type: 'text',align: 'left'
            },
            { display: '逻辑序号', name: 'id',width:0, align:'left', hide:true}
            ],
            enabledEdit: false, isScroll: true,rownumbers:true,
            onAfterShowData: f_onAfterShowData,
            width: '100%',height:350,checkbox:true,usePager:false
        });
    }

    
    //加载默认选中行
    function f_onAfterShowData(currentData){
    	var sumFeeAmt = 0;
    	if(currentData!=null){
	    	var arr = eval(currentData);      
	        var rows = arr.Rows;
            var manager = $("#cost1").ligerGetGridManager();
	        for(var i=0;i<rows.length;i++){
	        	if(rows[i].actualAmt!=null && rows[i].actualAmt != 0){
            		manager.select(i);
	        	}
	        	if(rows[i].feeClsCd == 1){
	        		sumFeeAmt = accAdd(sumFeeAmt , rows[i].feeAmt);
	        	}
	        }
	        $("#sumFeeAmt").val(sumFeeAmt);
    	}
    }

    //保存事件
    function saveFncPrjPayEvent(){
        	$("#repay_list_add_btn_id").click(function(event) {
        		save();
        	});
    };
	function cancelBtn(){
		 var TAB=top.mainApp.parentTab;
       var currentTabid=TAB.getSelectedTabItemID();
		 top.tab.removeTabItem(currentTabid);		
	}
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
		}
		return dateStr;
	}	

	//加法函数，用来得到精确的加法结果
	//说明：javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
	//调用：accAdd(arg1,arg2)
	//返回值：arg1加上arg2的精确结果
	function accAdd(arg1,arg2){
	 var r1,r2,rr,m,maxlen,result;
	 try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
	 try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}	 
	 console.log("length:"+r1+"_"+r2);
	 maxlen = Math.max(r1,r2);
	 m=Math.pow(10,maxlen);
	 console.log("Math.pow:"+m);
	 result = (arg1*m+arg2*m)/m;
	 console.log("result:"+result);
	 return result.toFixed(maxlen);
	}
	//保存更新
	function save(){
/*     	var actpymtdt = $("#actpymtdt").val();
    	console.log("saveFncPrjPayEvent actpymtdt:"+actpymtdt);
    	if(actpymtdt == null || actpymtdt == ""){
    		alert("请录入实际付款时间!");
    		return;
    	} */
 		if($("#repytp").val() == 1){
	 		if($("#recvAcc").val()=='' || $("#payAcc").val()==''){
				alert("请维护收付款账户！");
				return;
			} 			
 		}else{
	 		if($("#payAcc").val()==''){
				alert("请维护付款账户！");
				return;
			} 
 		}
        var data = $("form").serializeArray();
    	var jrnlid = $("#jrnlid").val();
		if(!jrnlid)return;

				var jsondata = '{jrnlid:'+jrnlid+',formdata:'+JSON.stringify(data)+'}';
            	$.ajax({
              	   type: "POST",
             	   url: "<%=path%>/rfbase/fncprjpaysch/updateFData.do",
             	   contentType: 'application/json',
             	   data: JSON.stringify(jsondata),
             	   dataType:"json",
	           		success:function(msg){
	        			 if(msg.msgcode == "0000"){
	      					 alert("保存成功!");
	           			 }else{
	          				UIUtils.alertErrorHold1(msg.errormsg);
	           			 }
	        		}
             	});
	}
	</script>
</body>
</html>